package sw6.digipecs.admin;

import java.util.List;

import sw6.digipecs.R;
import sw6.digipecs.model.Situation;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ManageSituationsActivity extends Activity implements OnItemSelectedListener {
	private ListView mList;
	private Context mContext;
	private List<Situation> mSituations;
	private String[] mSituationArray;
	private ArrayAdapter<String> mAdapter;
	private int mSelectedPos;

	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.managesituationsmainlayout); 
		initialise(); 
	}

	private void initialise() {
		mList = (ListView) findViewById(R.id.situationListView);
		mSelectedPos=0; 
		mList.setOnItemSelectedListener(this); 
		mContext=getApplicationContext(); 
		mSituationArray = new String[0]; 		
		UpdateList(); 
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater(); 
		inflater.inflate(R.menu.situationlistcontextmenu, menu); 		
		return true; 
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		MenuItem item = menu.getItem(1); 
		if(mSelectedPos!=0)
			item.setEnabled(true); 
		else 
			item.setEnabled(false); 
		return super.onPrepareOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.situationListContextMenuItemAdd:
			CreateSituation(); 
			return true; 
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void CreateSituation() {
		Intent intent = new Intent(this,CreateSituationActivity.class); 
		startActivityForResult(intent, CreateSituationActivity.CREATE_SITUATION_REQUEST_CODE); 
	}
	private void UpdateList()
	{
		mSituations=Situation.getAll(mContext); 
		mSituationArray = new String[mSituations.size()]; 
		int i = 0; 
		for(Situation s:mSituations)
		{
			mSituationArray[i]=s.getName(); 
			i++; 
		}
		mAdapter = new ArrayAdapter<String>(mContext, R.layout.situationlistrowlayout,mSituationArray); 
		mList.setAdapter(mAdapter); 
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case CreateSituationActivity.CREATE_SITUATION_REQUEST_CODE:
			UpdateList(); 
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		mSelectedPos=arg2; 
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		mSelectedPos=0; 
	}
}
